<template>
  <div class="pingfen">
      <!-- 按钮 -->
      <div class="btn-area">
        <el-button plain size="mini" type="primary" v-if="isAuth('add')" @click="audit">新增</el-button>
        <el-button plain size="mini" type="primary" v-if="isAuth('del')" @click="del">删除</el-button>
      </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table :data="tableData"  @row-click="rowClick" border header-row-class-name="tableHead" style="width: 100%" highlight-current-row :header-cell-style="{ textAlign: 'center',height: '60px' }" :cell-style="{ textAlign: 'center' }">
        <el-table-column prop="ruleProject" label="考核项目" width="300">
           <!-- <template slot-scope="scope">
              {{scope.row.ruleProject}}({{scope.row.ruleMaxscore}}分)
          </template> -->
        </el-table-column>
        <el-table-column prop="ruleStandard" label="评分标准" ></el-table-column>
        <el-table-column prop="ruleMaxscore" label="最高分" width="300" >
          <!-- <template slot-scope="scope">
              <el-form :model="scope.row" >
                  <el-form-item v-if="scope.row.ruleId"  prop='score'
                    :rules="score">
                    <el-input maxlength="2" @input="change()" v-model="scope.row.score" clearable size="mini" ></el-input>
                  </el-form-item>
              </el-form>
            </template> -->
        </el-table-column>
      </el-table>
      <!-- <el-table :data="tableDatab":show-header='header' border  header-row-class-name="tableHead" style="width: 100%" :header-cell-style="{ textAlign: 'center',height: '60px' }" :cell-style="{ textAlign: 'center' }">
        <el-table-column prop="project" label="" width="300"></el-table-column>
        <el-table-column prop="stander" label=""></el-table-column>
        <el-table-column prop="score" label="" width="300">
         <template slot-scope="scope">
            <el-form :model="scope.row" >
                <el-form-item v-if="scope.row.status"  prop='score' 
                :rules="score1">
                <el-input size="mini" @input="change()" v-model="scope.row.score" oninput ="value=value.replace(/[^{0-5}]/g,'')" maxlength="1" :disabled="scope.$index == (tableDatab.length - 1)"></el-input>
                </el-form-item>
                  <span v-else>{{scope.row.score}}</span>
            </el-form>
          </template>
        </el-table-column>
      </el-table> -->
      <!-- <div class="buttonBox">
        <button @click="submit">提交审核</button>
      </div> -->
      <!-- <div class="remark">
        <p>备注：<br/>
            1、总分100分，由风控部与储运中心共同评分，达60分及以上可列入准入名单。<br/>
            2、偿债能力指标和盈利能力指标的评价来源于《企业绩效评价标准值》——运输业行业指标（国务院国资委考核分配局编制）</p>
      </div> -->
    </div>
    <!-- 新增弹出框 -->
    <el-dialog
      v-el-drag-dialog
      :title="title"
      width="30%"
      :visible.sync="dialogFormVisible"
      v-if="dialogFormVisible"
      :close-on-click-modal="false"
      :destroy-on-close="true">
      <el-form label-width="10rem" ref="formData" :model="formData" :rules="addRules" size="mini">
        <div class="box">
          <el-row>
            <el-col :span="20">
              <el-form-item  label="考核项目：" prop="ruleProject">
                <el-input v-model="formData.ruleProject" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item  label="评分标准：" prop="ruleStandard">
                <el-input type="textarea" v-model="formData.ruleStandard" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item  label="最高分：" prop="ruleMaxscore">
                <el-input v-model="formData.ruleMaxscore" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div class="dialog-footer">
            <div class="btn">
              <el-button plain size="mini" type="primary" @click="close">关闭</el-button>
                <el-button plain size="mini" type="primary" @click="preserve">保存</el-button>
            </div>
        </div>

      </el-form>

    </el-dialog>
  </div>
</template>


<script>
import {insertScore,insertScoreListA,insertScoreAddA,insertScoreDel} from '@/request/api'
import isAuth from '@/mixins/buttonAuth'
export default {
  name: 'router.',
  mixins: [isAuth],
  data() {
     var fltCodevalidator1 = (rule, value, callback) => {
      var reg = /[0-9]/g
      if (value == reg) {
        callback()
      } 
      else if (!reg.test(value)) {
        callback(new Error('不符合评分标准'))
      } 
    }
    var fltCodevalidator2 = (rule, value, callback) => {
      var reg = /[012468]/g
      if (value == reg) {
        callback()
      } 
      else if (!reg.test(value)) {
        callback(new Error('不符合评分标准'))
      } else if(value > 10) {
        callback(new Error('评分标准  10分 8分 6分 4分 2-0分'))
      }else if(value < 0) {
        callback(new Error('评分标准  10分 8分 6分 4分 2-0分'))
      }
    }
    return {
      header:false,
      dialogFormVisible:false,
      title:'',
      formData:[],
      rowData:[],
      score1: [
        {
          required: true,
          validator: fltCodevalidator1,
          trigger: ['blur', 'change']
        }
      ],
       score: [
        {
          required: true,
          validator: fltCodevalidator2,
          trigger: ['blur', 'change']
        }
      ],
      lastinput:false,
      totalscore:'',
      tableData: [],
      tableDatab:[
        // {project: '总分', key:'type',score:this.totalscore,status:1,score:''},
      ],
      addRules:{
        ruleProject:[
          { required: true, message: '考核项目不能为空', trigger:'blur'}
        ],
        ruleStandard:[
           { required: true, message: '评分标准不能为空', trigger:'blur'}  
        ],
        ruleMaxscore:[
          { required: true, message: '请填写最高分', trigger:'blur'},
          {
          pattern:/^[0-9]+$/,
          message:'请输入自然数',
          trigger:'blur'
          }
        ]
      }
    }
  },
  created(){
     this.listData()
  },
  mounted(){
   
  },
  methods:{
    listData(){
      insertScoreListA().then(res=>{
        this.tableData  = res.data
      })
    },
    // 新增
    audit(){
      this.title = '新增'
      this.dialogFormVisible = true
      this.formData={
        ruleProject:'',
        ruleStandard:'',
        ruleMaxscore:''
      }
    },
    //删除
    del(){
       if(Object.keys(this.rowData).length===0){
        this.$alert('请选择需要删除的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }

       this.$confirm('您确认要删除此数据吗?', '提示', {
          closeOnClickModal: false
       }).then(()=>{
          insertScoreDel({ruleId:this.rowData.ruleId}).then(()=>{
            this.$message ({
              type: 'success',
              message: '删除成功'
            })
            this.listData()
          })
         
      })
 
    },
    //点击保存
    preserve(){
      this.$refs.formData.validate((valid)=>{
        if(valid){
          insertScoreAddA(this.formData).then(res=>{
              this.$message ({
                type: 'success',
                message: '新增成功'
              }) 
            this.listData()
            this.close()
          })
        }
      })
    },
    //关闭弹出框
    close(){
      this.dialogFormVisible = false
    },
    // 获取当前行数据
    rowClick(row){
      this.rowData = row
    },  
    change(){
      let newArr = []
      let scoreArr = []
      this.tableData.forEach(item => {

        if(item.score) {
          scoreArr.push(item.score)
        }else {
          newArr.push(item)
        }
      })
      this.tableDatab.forEach((items,index) => {
        if(index<this.tableDatab.length-1){
        if(items.score) {
          scoreArr.push(items.score)
        }else {
          newArr.push(items)
        }
        }
      })
      let numAarr = 0
      scoreArr.forEach(val => {
        numAarr += Number(val)
        this.tableDatab[this.tableDatab.length -1].score = String(numAarr)
      })

      this.totalscore=this.tableDatab[this.tableDatab.length -1].score
    },
    submit(){
      let errorArr=[]
      let newArr = []
      let scoreArr = []
      let typeArr = []
      this.tableData.forEach(item => {
        if(item.score==3||item.score==5||item.score==7||item.score==9||item.score>10||item.score<0){
          errorArr.push(item.score)
        }
        if(item.ruleProject){
          typeArr.push(item.ruleProject)
        }
        if(item.score) {
          scoreArr.push(item.score)
        }else{
          newArr.push(item)
        }
      })
      // this.tableDatab.forEach(items => {
      //   if(items.score) {
      //     scoreArr.push(items.score)
      //   }else {
      //     newArr.push(items)
      //   }
      // })
      if(newArr.length > 0) {
        this.$message({
            type:"error",
            message:"请输入分数"
          })
          return
      }
      if(errorArr.length > 0) {
        this.$message({
            type:"error",
            message:"输入分数不符合评分标准"
          })
          return
      }
      insertScore({score: scoreArr,project:typeArr,fltUuid:-1}).then(res => {
        if(res.success=="1"){
          this.$message({
            type:"success",
            message:"提交成功"
          })
        }
        // this.listData()
      })
    }
  }
}
</script>

<style lang="scss" scope>
.pingfen {
  .btn-area{
    padding-top: 20px;
    padding-left: 20px;
  }
  .tableBox {
    padding: 10px 20px;
    .buttonBox{
      height: 100px;
      line-height: 100px;
      text-align: center;
      button{
        width: 150px;
        height: 45px;
        background-color: #169BD5;
        cursor: pointer;
        color: #fff;
        font-size: 20px;
      }
    }
    .remark{
      p{
        font-size: 15px;
      }
    }
  }
  .dialog-footer{
    position: relative;
    width: 100%;
    height: 40px;
  }
  .btn{
      position: absolute;
      right:15px;
      top: 20px;
  }
}
.tableHead{
  font-size: 18px;
  letter-spacing:3px
}
.box{
  margin-left: 35px;
}
</style>
